package leetcode.f1t100;

import java.util.ArrayList;
import java.util.List;

/**
 * 顺时针内卷输出矩阵
 * https://leetcode.com/problems/spiral-matrix/
 *
 * @Author lichongxiao
 * @Date 2022/10/25 9:32
 */
public class Q54_SpiralMatrix {
    public List<Integer> spiralOrder(int[][] matrix) {
        int count = matrix.length * matrix[0].length, i = 0, j = 0, turn = 0;
        List<Integer> ans = new ArrayList<>();
        while (count > 0) {
            if (turn % 4 == 0) {
                ans.add(matrix[i][j]);
                count--;
                matrix[i][j++] = Integer.MAX_VALUE;
                if (j == matrix[0].length || matrix[i][j] == Integer.MAX_VALUE) {
                    turn++;
                    j--;
                    i++;
                }
            } else if (turn % 4 == 1) {
                ans.add(matrix[i][j]);
                count--;
                matrix[i++][j] = Integer.MAX_VALUE;
                if (i == matrix.length || matrix[i][j] == Integer.MAX_VALUE) {
                    turn++;
                    i--;
                    j--;
                }
            } else if (turn % 4 == 2) {
                ans.add(matrix[i][j]);
                count--;
                matrix[i][j--] = Integer.MAX_VALUE;
                if (j == -1 || matrix[i][j] == Integer.MAX_VALUE) {
                    turn++;
                    j++;
                    i--;
                }
            } else if (turn % 4 == 3) {
                ans.add(matrix[i][j]);
                count--;
                matrix[i--][j] = Integer.MAX_VALUE;
                if (i == -1 || matrix[i][j] == Integer.MAX_VALUE) {
                    turn++;
                    i++;
                    j++;
                }
            }
        }
        return ans;
    }

    public List<Integer> spiralOrder1(int[][] matrix) {

        List<Integer> ans = new ArrayList<>();
        int colBegin = 0, rowBegin = 0, colEnd = matrix.length - 1, rowEnd = matrix[0].length - 1, turn = 0;
        while (colBegin <= colEnd && rowBegin <= rowEnd) {
            switch (turn) {
                case 0:
                    for (int i = rowBegin; i <= rowEnd; i++) {
                        ans.add(matrix[colBegin][i]);
                    }
                    colBegin++;
                    break;
                case 1:
                    for (int i = colBegin; i <= colEnd; i++) {
                        ans.add(matrix[i][rowEnd]);
                        rowEnd--;
                    }
                    break;
                case 2: {
                    for (int i = rowEnd; i >= rowBegin; i--) {
                        ans.add(matrix[colEnd][i]);
                    }
                    break;
                }
                case 3:
                    for (int i = colEnd; i >= colBegin; i++) {
                        ans.add(matrix[i][rowBegin]);
                    }
                    break;
            }
            turn = turn++ % 4;
        }
        return ans;
    }

    public static void main(String[] args) {
        Q54_SpiralMatrix bean = new Q54_SpiralMatrix();
        List<Integer> list = bean.spiralOrder(new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}});
        System.out.println(list);
        list = bean.spiralOrder(new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}});
        System.out.println(list);
    }
}
